package com.zabra.zl.ui.verifyphone.presenter;

import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.verifyphone.contract.VerifyPhoneContract;
import com.zabra.zl.ui.verifyphone.model.VerifyPhoneModel;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class VerifyPhonePresenter extends BasePresenter<VerifyPhoneContract.View> implements VerifyPhoneContract.Presenter {
    private VerifyPhoneContract.Model model;

    public VerifyPhonePresenter() {
        model=new VerifyPhoneModel();
    }

    @Override
    public void getVerCodeData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getVerCodeData(body).enqueue(new Callback<SimpleBean>() {
            @Override
            public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body(),response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<SimpleBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }

    @Override
    public void getData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(body).enqueue(new Callback<SimpleBean>() {
            @Override
            public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<SimpleBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }
}
